//
//  TLMyBonusViewController.m
//  WXReader
//
//  Created by huaya on 2025/9/5.
//  Copyright © 2025 Andrew. All rights reserved.
//

#import "TLMyBonusViewController.h"
#import "TLMyBonusHeaderView.h"

#import "TLRechargeHistoryViewController.h"

#import "TLMyBonusTableViewCell.h"
#import "TLMyBonusModel.h"

#import "TLMyBonusListModel.h"

#import "TLMyBonusTipsView.h"

@interface TLMyBonusViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIView *titleView;
@property (nonatomic, strong) UIView *navView;
@property (nonatomic, strong) UILabel *navTitleLab;
@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) UIButton *messageBtn;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) TLMyBonusHeaderView *myBonusHeaderView;
@property (nonatomic, strong) UIView *tapView;

@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UIButton *centerBtn;
@property (nonatomic, strong) UIButton *rightBtn;

@property (nonatomic, assign) int selectedIndex;

@property (nonatomic, strong) TLMyBonusModel *headerModel;

@property (nonatomic, strong) NSNumber *pageNumb;
@property (nonatomic, strong) NSNumber *pageSize;
@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, strong) TLMyBonusTipsView *tipsView;
@property (nonatomic, strong) UIView *loadinView;
@end

@implementation TLMyBonusViewController
- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray new];
    }
    return _dataArr;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self getHeaderViewData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageNumb = @(1);
    self.pageSize = @(10);
    
    [self.view addGradientBackground];
    
//    [self.view addSubview:self.tableView];
    
    self.myBonusHeaderView = [TLMyBonusHeaderView createView];
    self.myBonusHeaderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 140+PUB_NAVBAR_HEIGHT);
    [self.view addSubview:self.myBonusHeaderView];
    
    [self.view addSubview:self.navView];
    
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.myBonusHeaderView.frame), kScreenWidth, 44)];
    [titleView setBackgroundColor: [UIColor whiteColor]];
    [self.view addSubview:titleView];
    self.titleView = titleView;
    
    UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(45, 0, kScreenWidth-45*2, 34)];
    [toolView setBackgroundColor:[UIColor hex_F5F5F5]];
    toolView.layer.cornerRadius = 17;
    [titleView addSubview:toolView];
    
    UIView *tapView = [[UIView alloc] init];
    tapView.frame = CGRectMake(8,4,toolView.frame.size.width/3 - 16,26);
    tapView.backgroundColor = [UIColor whiteColor];
    tapView.layer.cornerRadius = 26/2;
    tapView.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.0800].CGColor;
    tapView.layer.shadowOffset = CGSizeMake(0,2);
    tapView.layer.shadowOpacity = 1;
    tapView.layer.shadowRadius = 16;
    [toolView addSubview:tapView];
    self.tapView = tapView;
    
    UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, toolView.frame.size.width/3, 34)];
    [leftBtn setTitle:@"Acquired" forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor hex_999999] forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor hex_333333] forState:UIControlStateSelected];
    leftBtn.titleLabel.font = SYSTEM_FONT(16);
    [leftBtn addTarget:self action:@selector(switchBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    leftBtn.tag = 100;
    [toolView addSubview:leftBtn];
    self.leftBtn = leftBtn;
    
    UIButton *centerBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(leftBtn.frame), 0, toolView.frame.size.width/3, 34)];
    [centerBtn setTitle:@"Used" forState:UIControlStateNormal];
    [centerBtn setTitleColor:[UIColor hex_999999] forState:UIControlStateNormal];
    [centerBtn setTitleColor:[UIColor hex_333333] forState:UIControlStateSelected];
    centerBtn.titleLabel.font = SYSTEM_FONT(16);
    [centerBtn addTarget:self action:@selector(switchBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    centerBtn.tag = 200;
    [toolView addSubview:centerBtn];
    self.centerBtn = centerBtn;
    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(centerBtn.frame), 0, toolView.frame.size.width/3, 34)];
    [rightBtn setTitle:@"Expired" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor hex_999999] forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor hex_333333] forState:UIControlStateSelected];
    rightBtn.titleLabel.font = BOLD_SYSTEM_FONT(14);
    rightBtn.tag = 300;
    [rightBtn addTarget:self action:@selector(switchBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [toolView addSubview:rightBtn];
    self.rightBtn = rightBtn;
    
    [self.view addSubview:self.tableView];
    
}
- (void)switchBtnAction:(UIButton *)btn{
    
    [UIView animateWithDuration:0.5 animations:^{
        self.tapView.center = btn.center;
    }];
    
    self.selectedIndex = (int)btn.tag;
    
    [_tableView.mj_header beginRefreshing];
}

- (UIView *)navView{
    if (!_navView) {
        _navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, PUB_NAVBAR_HEIGHT)];
        [_navView setBackgroundColor:[UIColor clearColor]];
        _backBtn = [[UIButton alloc] initWithFrame:CGRectMake(6, PUB_NAVBAR_HEIGHT - 28 - 6, 28, 28)];
        [_backBtn setImage:[UIImage imageNamed:@"tzxqy_fh"] forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
        
        [_navView addSubview:_backBtn];
        
        _navTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_backBtn.frame)+10, PUB_NAVBAR_HEIGHT - 28 - 6, kScreenWidth-2*(CGRectGetMaxX(_backBtn.frame)+10), 28)];
        _navTitleLab.text = WXLocalizedString(@"我的钱包");
        _navTitleLab.textColor = [UIColor hex_333333];
        _navTitleLab.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        _navTitleLab.textAlignment = NSTextAlignmentCenter;
        [_navView addSubview:_navTitleLab];
        
        _messageBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth-18-16-16, PUB_NAVBAR_HEIGHT - 28 - 6, 28, 28)];
        [_messageBtn setImage:[UIImage imageNamed:@"wdzb_bz"] forState:UIControlStateNormal];
        [_messageBtn addTarget:self action:@selector(msgAction) forControlEvents:UIControlEventTouchUpInside];
        
        [_navView addSubview:_messageBtn];
        
    }
    return _navView;
}

- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)msgAction{
    
    self.tipsView = [TLMyBonusTipsView createView];
    
    [self.tipsView showInView:self.view andContent:self.headerModel.coinDescription];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.selectedIndex == 200) {
        return 80;
    }else{
        return 100;
    }
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TLMyBonusTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TLMyBonusTableViewCell"];
    
    if (indexPath.row<self.dataArr.count) {
        
        cell.model = self.dataArr[indexPath.row];
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        TLRechargeHistoryViewController *vc = [[TLRechargeHistoryViewController alloc] init];
        vc.selectedIndex = 0;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        TLRechargeHistoryViewController *vc = [[TLRechargeHistoryViewController alloc] init];
        vc.selectedIndex = 1;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (UITableView *)tableView{
    if (!_tableView) {
        UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width,MK_SCREEN_SAVE_BOTTOM_HEIGHT)];
        footView.backgroundColor = UIColor.clearColor;
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.titleView.frame), kScreenWidth, kScreenHeight-CGRectGetMaxY(self.titleView.frame)) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableFooterView = footView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = YES;
        if (@available(iOS 15.0, *)) {
            self.tableView.sectionHeaderTopPadding = 0;
        }
        
        [_tableView registerNib:[UINib nibWithNibName:@"TLMyBonusTableViewCell" bundle:nil] forCellReuseIdentifier:@"TLMyBonusTableViewCell"];
        
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadLastData)];
        header.lastUpdatedTimeLabel.hidden = YES;
        _tableView.mj_header = header;

        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        
    }
    return _tableView;
}

- (void)loadLastData{
    
    self.pageNumb = [NSNumber numberWithInt:1];
    [self.dataArr removeAllObjects];
    
    [self getListData];
}
- (void)loadMoreData{
    
    self.pageNumb = [NSNumber numberWithInt:(self.pageNumb.intValue+1)];
    
    [self getListData];
}
- (void)getHeaderViewData{
    
    self.loadinView = [TL_TopAlertManager showLoading:self.view];
    
    [APIManager sendGetRequest:[APIURLManager getMyBonusInfo] parameters:@{} model:TLMyBonusModel.class success:^(BOOL success, TLMyBonusModel *  _Nullable responseData, APIResponse * _Nonnull requestModel) {
        
        if (success) {
            self.headerModel = responseData;
            self.myBonusHeaderView.headerModel = responseData;
        }
        
        [self loadLastData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self.loadinView removeFromSuperview];
    }];
    
    
}
- (void)getListData{
    
    int status = 0;
    if (self.selectedIndex == 100) {
        status = 0;
    }else if (self.selectedIndex == 200) {
        status = 2;
    }else if (self.selectedIndex == 300) {
        status = 1;
    }
    
    [APIManager sendGetRequest:[APIURLManager getMyBonusList:status] parameters:@{} model:nil success:^(BOOL success, id  _Nullable responseData, APIResponse * _Nonnull requestModel) {
        if (self.loadinView) {
            [self.loadinView removeFromSuperview];
            self.loadinView = nil;
        }
        if (success) {
            
            NSArray *dataA = [TLMyBonusListModel mj_objectArrayWithKeyValuesArray:requestModel.data[@"records"]];
            
            [self.dataArr addObjectsFromArray:dataA];
            
            [self.tableView reloadData];
            
            [self.tableView.mj_header endRefreshing];
            if (dataA.count<self.pageSize.intValue) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.tableView.mj_footer endRefreshing];
            }
            
        }else{
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (self.loadinView) {
            [self.loadinView removeFromSuperview];
            self.loadinView = nil;
        }
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    }];
    
}
@end
